<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>proxy模式 &mdash; programming v0.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="top" title="programming v0.1 documentation" href="../index.html" />
    <link rel="up" title="设计模式学习笔记" href="index.html" />
    <link rel="next" title="设计模式总结" href="summary.html" />
    <link rel="prev" title="state模式" href="state.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="summary.html" title="设计模式总结"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="state.html" title="state模式"
             accesskey="P">上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" accesskey="U">设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="proxy">
<h1>proxy模式<a class="headerlink" href="#proxy" title="永久链接至标题">¶</a></h1>
<div class="section" id="id1">
<span id="index-9"></span><span id="id2"></span><h2>proxy模式定义<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
<p><strong>The Proxy pattern</strong> provides a surrogate or placeholder for
another object to control access to it.</p>
<p><strong>代理模式</strong> 为另一个对象提供了一个代理或者占位来控制对这个对象的访问.</p>
<p>使用代理模式来创建一个用于表示的对象来控制另一个对象的访问,
这个被控制的对象可能是 <strong>远程的</strong>, <strong>创建起来成本很高</strong>, <strong>或者有安全需求</strong>.</p>
</div>
<div class="section" id="id3">
<h2>问题描述<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p>图片从本地硬盘(或者远程机器)读取并且显示.</p>
<p>为了 <strong>控制访问</strong> (控制对本地硬盘的访问),我们可以使用 <strong>代理模式</strong>,
当所请求的图片还未载入时,我们创建新的真正图片的实例,并且载入,
如果已经载入,则直接显示即可.</p>
<p>具体的实现类图如下:</p>
<img alt="../image/image_class.jpg" src="../image/image_class.jpg" />
<p><strong>代理模式</strong> 的类图:</p>
<img alt="../image/proxy_class.png" src="../image/proxy_class.png" />
</div>
<div class="section" id="id4">
<h2>具体代码<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h2>
<p>下载请点击 <a href="../_downloads/proxy.cpp"><strong class="xref">这里</strong></a></p>
<div class="highlight-c++"><table class="highlighttable"><tr><td class="linenos"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67</pre></td><td class="code"><div class="highlight"><pre><span class="c">/*</span>
<span class="c"> * Author: Tower Joo&lt;zhutao.iscas@gmail.com&gt;</span>
<span class="c"> * Time: 2009-06-11 20:18</span>
<span class="c"> * File: proxy.cpp</span>
<span class="c"> * Description: This source file is to implement the proxy design pattern.</span>
<span class="c"> */</span>

<span class="cp">#include &lt;iostream&gt;</span>
<span class="cp">#include &lt;string&gt;</span>
<span class="cp">#include &lt;vector&gt;</span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Image</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="k">virtual</span> <span class="kt">void</span> <span class="n">displayImage</span><span class="p">(){}</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">RealImage</span><span class="o">:</span><span class="k">public</span> <span class="n">Image</span>
<span class="p">{</span>
    <span class="k">private</span><span class="o">:</span>
        <span class="n">string</span> <span class="n">filename</span><span class="p">;</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">RealImage</span><span class="p">(</span><span class="n">string</span> <span class="n">name</span><span class="p">)</span><span class="o">:</span><span class="n">filename</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">loadImageFromDisk</span><span class="p">();</span>    <span class="c">// an expensive operation, may from network</span>
        <span class="p">}</span>
        <span class="kt">void</span> <span class="n">displayImage</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Displaying Image &quot;</span><span class="o">&lt;&lt;</span><span class="n">filename</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="kt">void</span> <span class="n">loadImageFromDisk</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Loading &quot;</span><span class="o">&lt;&lt;</span><span class="n">filename</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
        <span class="p">}</span>
<span class="p">};</span>

<span class="c">// the proxy which provide the access control to the expensive operation(load file from disk)</span>
<span class="k">class</span> <span class="nc">ImageProxy</span><span class="o">:</span><span class="k">public</span> <span class="n">Image</span>
<span class="p">{</span>
    <span class="k">private</span><span class="o">:</span>
        <span class="n">string</span> <span class="n">filename</span><span class="p">;</span>
        <span class="n">Image</span> <span class="o">*</span><span class="n">image</span><span class="p">;</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">ImageProxy</span><span class="p">(</span><span class="n">string</span> <span class="n">name</span><span class="p">)</span><span class="o">:</span><span class="n">filename</span><span class="p">(</span><span class="n">name</span><span class="p">){</span><span class="n">image</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;}</span>
        <span class="kt">void</span> <span class="n">displayImage</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">image</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">image</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RealImage</span><span class="p">(</span><span class="n">filename</span><span class="p">);</span>
            <span class="p">}</span>
            <span class="n">image</span><span class="o">-&gt;</span><span class="n">displayImage</span><span class="p">();</span>
        <span class="p">}</span>
<span class="p">};</span>

<span class="kt">int</span> <span class="n">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">ImageProxy</span> <span class="n">image1</span><span class="p">(</span><span class="s">&quot;image1&quot;</span><span class="p">);</span>
    <span class="n">ImageProxy</span> <span class="n">image2</span><span class="p">(</span><span class="s">&quot;image2&quot;</span><span class="p">);</span>
    <span class="n">ImageProxy</span> <span class="n">image3</span><span class="p">(</span><span class="s">&quot;image3&quot;</span><span class="p">);</span>

    <span class="n">image1</span><span class="p">.</span><span class="n">displayImage</span><span class="p">();</span>
    <span class="n">image2</span><span class="p">.</span><span class="n">displayImage</span><span class="p">();</span>
    <span class="n">image1</span><span class="p">.</span><span class="n">displayImage</span><span class="p">();</span>  <span class="c">// this will not load the file for the second time</span>

	<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</td></tr></table></div>
<p>输出结果为:</p>
<div class="highlight-python"><pre>Loading image1
Displaying Image image1
Loading image2
Displaying Image image2
Displaying Image image1</pre>
</div>
</div>
<div class="section" id="id5">
<h2>面对新的需要<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h2>
</div>
<div class="section" id="id6">
<h2>更多注意<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h2>
<p>通常有多种 <strong>代理模式</strong> 的变种,常用的包括:</p>
<ul class="simple">
<li>Remote Proxy : 作为远程的代理</li>
<li>Virtual Proxy: 对一些expensive resource的访问访问控制</li>
<li>Protection Proxy: 根据权限提供特定的服务(方法)</li>
</ul>
<p><a class="reference external" href="decorator.html#id1"><em>Decorator Pattern</em></a>, <a class="reference external" href="facade.html#id1"><em>Facade Pattern</em></a>, <a class="reference internal" href="#id1"><em>Proxy Pattern</em></a>, <a class="reference external" href="adapter.html#id1"><em>Adapter Pattern</em></a>
的比较:</p>
<ul class="simple">
<li><strong>Decorator</strong> - Wraps another object and provides additional behavior for it</li>
<li><strong>Facade</strong> - Wraps a bunch of objects to simplify their interface</li>
<li><strong>Proxy</strong> - Wraps another object to control access to it</li>
<li><strong>Adapter</strong> - Wraps another object and provides a different interface to it</li>
</ul>
<p><strong>proxy pattern</strong> 同样也会增加类的数目.</p>
</div>
<div class="section" id="id7">
<h2>参考更多<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h2>
<ol class="arabic simple">
<li><a class="reference external" href="http://en.wikipedia.org/wiki/Proxy_pattern">Proxy_Pattern</a></li>
</ol>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference external" href="">proxy模式</a><ul>
<li><a class="reference external" href="#id1">proxy模式定义</a></li>
<li><a class="reference external" href="#id3">问题描述</a></li>
<li><a class="reference external" href="#id4">具体代码</a></li>
<li><a class="reference external" href="#id5">面对新的需要</a></li>
<li><a class="reference external" href="#id6">更多注意</a></li>
<li><a class="reference external" href="#id7">参考更多</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="state.html"
                                  title="上一章">state模式</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="summary.html"
                                  title="下一章">设计模式总结</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/designpattern/proxy.txt"
                     rel="nofollow">显示源代码</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>快速搜索</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="搜索" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              输入相关的模块，术语，类或者函数名称进行搜索
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="summary.html" title="设计模式总结"
             >下一页</a> |</li>
        <li class="right" >
          <a href="state.html" title="state模式"
             >上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" >设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; 版权所有 2009, zhutao.iscas@gmail.com.
      使用 <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>